package com.example.mylazyviewpager.base;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public abstract class LazyFragment3 extends Fragment {
    private final String TAG = "LazyFragment3";
    private int index = -1;
    private View rootView;
    protected boolean isViewCreated = false;

    public LazyFragment3(int index) {
        this.index = index;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onCreateView: " + index);
        if (rootView == null){
            rootView = inflater.inflate(getLayoutRes(), container, false);
        }
        isViewCreated = true;
        initView(rootView);

        if (getUserVisibleHint()){
            setUserVisibleHint(true);
        }
        return rootView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.e(TAG, "setUserVisibleHint: isVisibleToUser " + isVisibleToUser +" "+index);
        if (isViewCreated){
            if (isVisibleToUser){
                dispatchUserVisibleHint(true);
            } else {
                dispatchUserVisibleHint(false);
            }
        }
    }

    private void dispatchUserVisibleHint(boolean visibleState) {
        if (visibleState){
            //加载数据
            onFragmentLoad();
        } else {
            //停止一切操作
            onFragmentStopLoad();
        }
    }

    protected  void  onFragmentStopLoad(){
        Log.e(TAG, "onFragmentStopLoad: " + index);
    }

    protected  void onFragmentLoad(){
        Log.e(TAG, "onFragmentLoad: " + index);
    }

    protected abstract void initView(View rootView);

    protected abstract int getLayoutRes();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate: " + index);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: " + index);
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: " + index);
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: " + index);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: " + index);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e(TAG, "onStart: " + index);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.e(TAG, "onAttach: " + index);
    }

    @Override
    public void onAttachFragment(Fragment childFragment) {
        super.onAttachFragment(childFragment);
        Log.e(TAG, "onAttachFragment: " + index);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.e(TAG, "onActivityCreated: " + index);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.e(TAG, "onDestroyView: " + index);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.e(TAG, "onDetach: " + index);
    }
}
